package com.ambition.demo.design.proxy;


import com.ambition.demo.design.proxy.service.OrderService;
import com.ambition.demo.design.proxy.service.impl.OderServiceImpl;
import com.ambition.demo.utils.ProxyUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * jdk动态代理 用于获取目标方法执行时间
 */

public class JdkDynamicProxy implements InvocationHandler {

    private Object target;

    public JdkDynamicProxy(Object target) {
        this.target = target;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        long l = System.currentTimeMillis();

        Object invoke = method.invoke(target, args);

        long l1 = System.currentTimeMillis();
        System.out.println("耗时："+(l1-l));
        return invoke;
    }

    public static void main(String[] args) {
        OrderService target = new OderServiceImpl();
        OrderService orderServiceProxy = (OrderService) ProxyUtil.newProxyInstance(target);
        orderServiceProxy.createOrder();
        orderServiceProxy.detail();
        orderServiceProxy.modify();
    }
}
